/*
Copyright 2011, CSE Shareware Foundation
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice, 
   this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, 
   this list of conditions and the following disclaimer in the documentation 
   and/or other materials provided with the distribution.
 * Neither the name of CSE Shareware Foundation nor the names of its contributors 
   may be used to endorse or promote products derived from this software without 
   specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef _CHARARRAY_H_CSEAPI_
#define _CHARARRAY_H_CSEAPI_

class TIntObj;
class TObjArray;

class CSE_IMPEXP_IN TCharArray
{
private:
    TBool copyFrom(const TXchar* pSour, int iLen, int iOff) const;
    TCharArray& joinEx(const TXchar* pSour, int iSourLen);
    void insertEx(int iPos, const TXchar *pSour, int iLen);
    TCharArray& tailJoin(const TXchar* pSour, int iSourLen=-1);

protected:
    TXchar *m_data;
    TXchar *transfer(TXchar *pOther);

public:
    TCharArray();
    TCharArray(const TCharArray& s);
    TCharArray(int MagicNum, TCharArray& str);
    
    TCharArray(TXchar ch, int count=1);
    TCharArray(const TXchar* pSour, int n=-1, int iExt=0);
    ~TCharArray();
    
    TCharArray& join(const TCharArray& str, int index=0);

    TCharArray& operator=(const TCharArray& str);
    TCharArray& operator=(const TXchar* pSour);
    TCharArray& operator=(TXchar ch);

    void operator+=(const TCharArray& str);
    void operator+=(const TXchar* pSour);
    void operator+=(TXchar ch);
    TCharArray multi(int n) const;
    
    TXchar& sub(int index) const;         // same to operator[]
    TXchar& operator[](int index) const;
    operator TXchar*() const;
    
    TXchar *addr() const;
    int len() const;
    TBool isEmpty() const;
    TInt compare(const TCharArray& sOther) const;
    TInt compare(const TXchar *pOther) const;

    TCharArray slice(int index, int iLen=-1) const;

    void exchange(TCharArray& other);
    TXchar append(TXchar ch);
    TCharArray& add(TXchar ch, int *pIndex=NULL);
    const TIntObj& insert(int index, const TIntObj& obj);
    void insertSlice(int index, const TXchar *pSour, int iLen=-1);
    void remove(int index, int iLen=1);

    void clear();
    int truncate(int iLen) const;
    int sortAdd(TXchar ch);
    void sort() const;

    static TCharArray vformat(const TXchar *frmt, va_list ArgPtr);
    static TCharArray format(const TXchar *frmt, ...);

    void resize(int iSize);
    TUint32 hash() const;
    TCharArray str(TXchar pre=_T('"')) const;

    TBool toInt(TInt& ret) const;
    TBool toWint(TInt64& ret) const;
    TBool toDouble(TDouble& ret) const;

    const TCharArray& upper() const;
    const TCharArray& lower() const;
    TCharArray toUpper() const;
    TCharArray toLower() const;

    int trim() const;
    int trimLeft() const;
    int trimRight() const;
    int find(TXchar ch, int from=0) const;
    int find(const TXchar *sSub, int from=0) const;
    
    TBool enumItem(void*& pIter, TXchar *pRet=NULL) const;
    
    int count(const TXchar* sSub, int iStart=0, int iEnd=0) const;
    TCharArray& align(int iWidth, TXchar fillChar = _T(' '));
    TCharArray& alignLeft(int iWidth, TXchar fillChar = _T(' '));
    TCharArray& alignRight(int iWidth, TXchar fillChar = _T(' '));
    TCharArray& replace(const TXchar *sSub, const TXchar *sNew, int count=-1);
    
    TObjArray split(const TXchar* sSep=NULL, int count=-1) const;
    TObjArray splitLines(TBool keepEnds=false, int count=-1) const;

    TCharArray operator+(const TCharArray& y) const;
    TCharArray operator+(const TXchar *y) const;
    TCharArray operator+(TXchar y) const;

    TBool operator==(const TCharArray& y) const;
    TBool operator==(const TXchar *y) const;
    TBool operator!=(const TCharArray& y) const;
    TBool operator!=(const TXchar *y) const;

    TBool operator>(const TCharArray& y) const;
    TBool operator>(const TXchar *y) const;
    TBool operator>=(const TCharArray& y) const;
    TBool operator>=(const TXchar *y) const;

    TBool operator<(const TCharArray& y) const;
    TBool operator<(const TXchar *y) const;
    TBool operator<=(const TCharArray& y) const;
    TBool operator<=(const TXchar *y) const;
};

extern CSE_IMPEXP_IN const TCharArray& CseEmptyStr;

extern CSE_IMPEXP_IN TCharArray Cse_StrFromAnsi(const char *sour, int n=-1);
extern CSE_IMPEXP_IN TCharArray Cse_StrFromUnicode(const TWchar *sour, int n=-1);

extern CSE_IMPEXP_IN TCharArray operator+(TXchar x, const TCharArray& y);
extern CSE_IMPEXP_IN TCharArray operator+(const TXchar *x, const TCharArray& y);

extern CSE_IMPEXP_IN TBool operator==(const TXchar *x, const TCharArray& y);
extern CSE_IMPEXP_IN TBool operator!=(const TXchar *x, const TCharArray& y);
extern CSE_IMPEXP_IN TBool operator>(const TXchar *x, const TCharArray& y);
extern CSE_IMPEXP_IN TBool operator>=(const TXchar *x, const TCharArray& y);
extern CSE_IMPEXP_IN TBool operator<(const TXchar *x, const TCharArray& y);
extern CSE_IMPEXP_IN TBool operator<=(const TXchar *x, const TCharArray& y);

#endif
